package com.haixiaoke.saas.enterprise.bill.controller;

import java.util.List;

import cn.hutool.core.util.StrUtil;
import com.haixiaoke.saas.bill.domain.Bill;
import com.haixiaoke.saas.bill.service.IBillService;
import com.haixiaoke.saas.common.annotation.Log;
import com.haixiaoke.saas.common.constant.PlatformConstants;
import com.haixiaoke.saas.common.core.domain.AjaxResult;
import com.haixiaoke.saas.common.core.page.TableDataInfo;
import com.haixiaoke.saas.common.enums.BusinessType;
import com.haixiaoke.saas.system.BaseController;
import com.haixiaoke.saas.system.framework.security.util.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 账单Controller
 * 
 * @author panda
 * @date 2025-06-26
 */
@RestController
@RequestMapping("/enterprise/bill/bill")
public class EnterpriseBillController extends BaseController {
    @Autowired
    private IBillService billService;

    /**
     * 查询账单列表
     */
    @PreAuthorize("@ss.hasPermi('enterprise:bill:page')")
    @GetMapping("/page")
    public TableDataInfo list(Bill bill) {
        startPage();
        bill.setTenantType(getLoginUser().getTenantId());
        if (StrUtil.equals(PlatformConstants.PC_BUSINESS, getLoginUser().getPlatform())) {
            bill.setTenantIds(getTenantIds());
        }
        if (StrUtil.equals(PlatformConstants.MP_WECHAT_BUSINESS, getLoginUser().getPlatform())) {
            bill.setTenantId(getLoginUser().getTenantId());
        }
        List<Bill> list = billService.selectBillList(bill);
        return getDataTable(list);
    }

    /**
     * 获取账单详细信息
     */
    @PreAuthorize("@ss.hasPermi('enterprise:bill:query')")
    @GetMapping(value = "/{billId}")
    public AjaxResult getInfo(@PathVariable("billId") String billId) {
        return success(billService.selectBillByBillId(billId));
    }

    /**
     * 新增账单
     */
    @PreAuthorize("@ss.hasPermi('enterprise:bill:add')")
    @Log(title = "账单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Bill bill) {
        bill.setCreateBy(getUsername());
        bill.setTenantId(getLoginUser().getTenantId());
        return billService.insertBill(bill);
    }

    /**
     * 完成账单 未付款->已付款
     */
    @PreAuthorize("@ss.hasPermi('enterprise:bill:edit')")
    @Log(title = "账单", businessType = BusinessType.UPDATE)
    @PutMapping("/completed")
    public AjaxResult completed(@RequestBody Bill bill) {
        bill.setUpdateBy(getUsername());
        bill.setTenantId(getLoginUser().getTenantId());
        return billService.completed(bill);
    }

    /**
     * 取消账单 未付款->已取消
     */
    @PreAuthorize("@ss.hasPermi('enterprise:bill:edit')")
    @Log(title = "账单", businessType = BusinessType.UPDATE)
    @PutMapping("/cancel/{billId}")
    public AjaxResult cancel(@PathVariable String billId) {
        return billService.cancel(billId, getUsername(), getLoginUser().getTenantId());
    }
}
